package linearList;

public class Demo {

	public static void main(String[] args) {
//		String postfix="1 2 3 4 -*+51 +";
//		System.out.println(toValue(postfix));
//		String infix="1+2*(3-4*(5+6)/7+8)-(9+10)*11";
//		System.out.println(toPostfix(infix));
		
//		PriorityQueue pri=new PriorityQueue();
//		pri.add(new PriQueNode('E',4));
//		pri.add(new PriQueNode('D',3));
//		pri.add(new PriQueNode('A',1));
//		pri.add(new PriQueNode('F',3));
//		pri.add(new PriQueNode('B',2));
//		pri.add(new PriQueNode('C',1));
//		System.out.println(pri.poll().data);
//		System.out.println(pri.poll().data);
//		System.out.println(pri.poll().data);
//		System.out.println(pri.poll().data);
//		System.out.println(pri.poll().data);
//		System.out.println(pri.poll().data);
		
		CrossLinkedMatrix clm=new CrossLinkedMatrix(5, 5);
		clm.setElem(3, 0, 10);
		clm.setElem(1, 0, 5);
		clm.setElem(1, 3, 14);
		clm.setElem(2, 0, 4);
		System.out.println(clm.toString());

	}
	
	public static int toValue(String postfix) {
		LinkedStack stack=new LinkedStack();
		char ch;
		for(int i=0;i<postfix.length();) {
			ch=postfix.charAt(i);
			if(ch>='0'&&ch<='9') {
				int v=0;
				while(ch!=' ') {
					v=v*10+ch-'0';
					i++;
					ch=postfix.charAt(i);
				}
				stack.push(v);
			}
			else if(ch==' ') {
				i++;
			}else {
				int x,y;
				x=stack.pop();
				y=stack.pop();
				switch (ch) {
				case '+':
					stack.push(y+x);					
					break;
				case '-':
					stack.push(y-x);					
					break;
				case '*':
					stack.push(y*x);					
					break;
				case '/':
					stack.push(y/x);					
					break;
				default:
					break;
				}
				i++;
			}
		}
		return stack.pop();
	}
	
	public static String toPostfix(String infix) {
		SeqStack stack=new SeqStack(infix.length());
		StringBuffer postfix=new StringBuffer(infix.length()*2);
		int i=0;
		while(i<infix.length()) {
			char ch=infix.charAt(i);
			switch(ch) {
			case '+':
			case '-':
				while(!stack.isEmpty()&&!stack.getTop().equals("(")) {
					postfix.append(stack.pop());
				}
				stack.push(ch+"");
				i++;
				break;
			case '*':
			case '/':
				while(!stack.isEmpty()&&(stack.getTop().equals("*")||stack.getTop().equals("/"))) {
					postfix.append(stack.pop());
				}
				stack.push(ch+"");
				i++;				
				break;
			case '(':
				stack.push(ch+"");
				i++;
				break;
			case ')':
				String op=stack.pop();
				while(!stack.isEmpty()&&!op.equals("(")) {
					postfix.append(op);
					op=stack.pop();
				}
				i++;
				break;
			default:
				while(ch>='0'&&ch<='9') {
					postfix.append(ch);
					i++;
					if(i<infix.length()) ch=infix.charAt(i);
					else break;
				}
				postfix.append(" ");
			}
		}
		while(!stack.isEmpty()) {
			postfix.append(stack.pop());
		}
		return postfix.toString();
	}
	
	public static String toPostfixNew(String infix) {
		SeqStack stack=new SeqStack(infix.length());
		StringBuffer postfix=new StringBuffer(infix.length()*2);
		int i=0;
		while(i<infix.length()) {
			char ch=infix.charAt(i);
			switch(ch) {
			case '+':
			case '-':
//				postfix.append(" ");
				while(!stack.isEmpty()&&!stack.getTop().equals("(")) {
					postfix.append(stack.pop());
				}
				stack.push(ch+"");
				i++;
				break;
			case '*':
			case '/':
//				postfix.append(" ");
				while(!stack.isEmpty()&&(stack.getTop().equals("*")||stack.getTop().equals("/"))) {
					postfix.append(stack.pop());
				}
				stack.push(ch+"");
				i++;				
				break;
			case '(':
				stack.push(ch+"");
				i++;
				break;
			case ')':
				String op=stack.pop();
				while(!stack.isEmpty()&&!op.equals("(")) {					
					postfix.append(op);
					op=stack.pop();
				}
				i++;
				break;
			default:
				while(i<infix.length()&&ch>='0'&&ch<='9') {
					postfix.append(ch);
					i++;
					if(i<infix.length()) ch=infix.charAt(i);
				}
				postfix.append(" ");
			}
		}
		while(!stack.isEmpty()) {			
			postfix.append(stack.pop());			
		}		
		return postfix.toString();
	}
}
